package com.cdinternationalcompany.syhapp.push.vivo

import android.content.Context
import android.util.Log
import com.cdinternationalcompany.syhapp.push.Constants.Companion.tag
import com.cdinternationalcompany.syhapp.push.NotificationHelper
import com.cdinternationalcompany.syhapp.push.TokenObserver
import com.cdinternationalcompany.syhapp.push.xiaomi.XiaoMiPushMessageReceiver
import com.cdinternationalcompany.syhapp.utils.ChannelManager
import com.vivo.push.model.UPSNotificationMessage
import com.vivo.push.sdk.OpenClientPushMessageReceiver
import org.json.JSONObject
import java.lang.Exception

class VivoPushReceiver : OpenClientPushMessageReceiver() {

    companion object {
        val TAG = "$tag.Vivo"
    }


    override fun onReceiveRegId(context: Context?, regId: String?) {
        super.onReceiveRegId(context, regId)
        TokenObserver.instance.collectionToken(context!!)
    }

    override fun onNotificationMessageClicked(context: Context?, msg: UPSNotificationMessage?) {
        super.onNotificationMessageClicked(context, msg)
        if (msg == null) {
            return
        }
        try {
            val json = JSONObject(msg.content)
            val para = mutableMapOf<String,String>()
            for (key in json.keys()) {
                val content = json.getString(key)
                para[key] = content.toString()
                Log.i(VivoPushReceiver.TAG, "receive data from push, key = $key, content = $content")
            }
            ChannelManager.instance.callFlutter(
                    "PushApi/receiveMessage", para,
            )
        } catch (e: Exception) {

            Log.d(VivoPushReceiver.TAG, "onNotificationMessageClicked -> ${msg.content}")
        }


        Log.d(TAG, "Notification clicked content -> ${msg.content}")
//        NotificationHelper.openPage(msg.content, context)
    }

}